import type { ArticleDataRes, ArticleParams, UploadUrl } from '@/types/article'
import type { PublishArticleRes } from '@/types/Public'
import service, { request } from '@/utils/request'

// 获取文章列表
export const getArticleListAPI = (params: ArticleParams) => {
  return request<ArticleDataRes>('/v1_0/mp/articles', 'GET', params)
}

// 删除文章
export const delArticleAPI = (id: string) => {
  return request(`/v1_0/mp/articles/${id}`, 'DELETE')
}

// 上传图片
export const UpLoadImageAPI = (image: FormData) => {
  return request<UploadUrl>('/v1_0/upload', 'POST', image)
}

// 回显数据-获取文章详情
export const getArticleDetailAPI = async (id: string) => {
  return request<PublishArticleRes>(`/v1_0/mp/articles/${id}`, 'GET')
}

// 发表文章
export const publishAPI = async (params: PublishArticleRes, draft?: boolean) => {
  return await service.post<PublishArticleRes>('/v1_0/mp/articles', params, { params: { draft } })
}

// 编辑文章
// export const EditArticleDataAPI = (data: PublishArticleRes, target: string, draft: boolean) => {
//   return service.request({
//     url: `/v1_0/mp/articles/${target}`,
//     method: 'PUT',
//     data: data,
//     params: {
//       draft,
//     },
//   })
// }

export const EditArticleDataAPI = (data: PublishArticleRes, target: string, draft: boolean) => {
  return request(`/v1_0/mp/articles/${target}`, 'PUT', data, { params: { draft } })
}
